<?php
/**
 * @author krystian.kuczek@mediaflex.pl
 * @copyright Mediaflex Sp. z O.O. 2010
 */
class DictionaryController extends MainController implements ControllerInterface {
	const PARSE_LANG = true ;

	//o - order
	//v - order vector
	//t - total items to select
	public $tableConfig = array(
		'dictionary_table' => array(
			'o' => array(
				'etykieta' => "mf_dictionary.label",
				'tlumaczenie' => 'mf_dictionary.translation',
				'jezyk' => 'mf_dictionary.lang',
				'_' => 'etykieta'
			),
			'v' => 'rosnaco',
			't' => 50
		)
	);

	public function getMainClassName() {
		return 'MfDictionary';
	}

	public function IndexAction($param) {
		$this->AddScript('Admin/mf.dictionary.js');
		$this->smarty->assign('table_type','dictionary_table');
//		$oTable = array(
//			'etykieta' => "mf_dictionary.label",
//			'tlumaczenie' => 'mf_dictionary.translation',
//		);
//
//		if (self::PARSE_LANG) {
//			$oTable['jezyk'] = 'mf_dictionary.lang';
//		}

//		$list = $this->GetList('MfDictionary',
//				array('dictionary_table', $param, array('o' => 'etykieta', 'v' => 'malejaco', 't' => 10)),
//				$oTable, array('malejaco' => 'DESC', 'rosnaco' => 'ASC'));

		$list = MfDictionaryDAL::GetResult(MfDictionaryDAL::GetDalDataObj(array('tableData' => $this->tableParams['dictionary_table'])));

		$this->smarty->assign('list', $list);

		$this->smarty->assign('c',MfDictionaryDAL::getResult(MfDictionaryDAL::GetDalDataObj(array('count' => 1))));
	}

	public function AjaxEditAction($param) {
		$this->SetAjaxRender();
		
		if (Request::IsPost()) {
			try {
				$post = Request::GetAllPost();

				$ret = true;

				if (!trim($post['text'])) {
					$ret = array('response' => false, 'message' => 'Musisz podać tłumaczenie.');
				}

				$dalData = MfDictionaryDAL::GetDalDataObj();
				$dalData->addCondition('id_mf_dictionary', $post['id']);
				$dalData->SetLimit(1);

				$dictionaryObj = MfDictionaryDAL::GetObj($dalData);

				if (!($dictionaryObj instanceof MfDictionary)) {
					$ret = array('response' => false, 'message' => 'Nie znaleziono tego tłumaczenia.');
				}

				$dictionaryObj->SetTranslation($post['text']);

				MfDictionaryDAL::Save($dictionaryObj->GetDalDataObj());
			} catch (Exception $e) {
				$ret = array('response' => false, 'message' => 'Nie znaleziono tłumaczenia.');
			}

			$this->content = json_encode($ret);
		} else {
			$this->content = json_encode(false);
		}
	}

	public function ValidateAddAction($param, $asAction = true) {

		if ($asAction) {
			$data = Request::GetAllPost(false);
		} else {
			$data = $param['data'];
		}

		$validator = new Validator($data);

		$validator->IsEmpty('label', 'Etykieta jest wymagana');
		$validator->IsEmpty('translation', 'Tłumaczenie jest wymagane');

		if (self::PARSE_LANG) {
			$validator->IsEmpty('language', 'Język jest wymagany');
		}

		return $validator;
		
	}

	public function AddAction($param) {
		$this->partialTemplate = 'Edit.tpl';

		if (Request::IsPost()) {
			$data = Request::GetAllPost();

			$param['data'] = $data;

			$validator = $this->ValidateAddAction($param, false);

			if ($validator->IsError()) {
				$this->smarty->assign('errorList', $validator->GetErrorList());
			} else {
				$dictionaryObj = new MfDictionary();
				$dictionaryObj->setLabel($data['label']);

				//Wycinamy p z wyswiga
				$translation = trim($data['translation']);

				if (substr($translation, 0, 3) == "<p>") {
					$translation = substr($translation, 3);
				}
				
				if (substr($translation, -4) == "</p>") {
					$translation = substr($translation, 0, -4);
				}

				$dictionaryObj->setTranslation($translation);

				if (self::PARSE_LANG) {
					$dictionaryObj->setLang($data['lang']);
				} else {
					$dictionaryObj->setLang(Router::$curLang);
				}

				try {
					$id = MfDictionaryDAL::Save($dictionaryObj->GetDalDataObj());
					$this->addRedirectInfo((!self::PARSE_LANG ? "Komunikat został dodany" : "Tłumaczenie zostało dodane"), 'ok', array('Dictionary' => 'Index'));
				} catch (MysqlException $e) {
					if ($e->GetCode() == 1062) {
						$validator->addError('label', 'Identyfikator o tej nazwie już istnieje');
						$this->smarty->assign('errorList', $validator->GetErrorList());
					}
				}
			}
		}
	}

	public function EditAction($param) {
		if (!isset($param['id'])) {
			$this->AddRedirectInfo('Brak id', 'error', array('Dictionary' => 'Index'));
			return;
		}

		$dalData = MfDictionaryDAL::GetDalDataObj();
		$dalData->addCondition('id_mf_dictionary', $param['id']);
		$dalData->SetLimit(1);
		
		$dictionaryObj = MfDictionaryDAL::GetObj($dalData);

		if (!$dictionaryObj || !($dictionaryObj instanceof MfDictionary)) {
			$this->AddRedirectInfo('W słowniku nie ma takiego wpisu', 'error', array('Dictionary' => 'Index'));
			return;
		}

		$this->smarty->assign('dictionaryObj', $dictionaryObj);

		if (Request::IsPost()) {
			$data = Request::GetAllPost(false);

			$param['data'] = $data;

			$validator = $this->ValidateAddAction($param, false);

			if (1 == 0) {
				$this->smarty->assign('errorList', $validator->GetErrorList());
			} else {
				$dictionaryObj->setLabel($data['label']);
				$translation = trim($data['translation']);

				if (substr($translation, 0, 3) == "<p>") {
					$translation = substr($translation, 3);
				}
				
				if (substr($translation, -4) == "</p>") {
					$translation = substr($translation, 0, -4);
				}
				
				$dictionaryObj->setTranslation($translation);

				if (self::PARSE_LANG) {
					$dictionaryObj->setLang($data['lang']);
				} else {
					$dictionaryObj->setLang(Router::$curLang);
				}

				try {
					$id = MfDictionaryDAL::Save($dictionaryObj->GetDalDataObj());
					$this->addRedirectInfo((!self::PARSE_LANG ? "Komunikat został dodany" : "Tłumaczenie zostało dodane"), 'ok', array('Dictionary' => 'Index'));
				} catch (MysqlException $e) {
					if ($e->GetCode() == 1062) {
						$validator->addError('label', 'Identyfikator o tej nazwie już istnieje');
						$this->smarty->assign('errorList', $validator->GetErrorList());
					}
				}
			}
		}

		
	}

	public function PostDispatch($param) {

	}

	public function PreDispatch($param) {
        $this->RunShared('Auth');
        $this->Run($param);
	}

	

}
?>
